package com.xiton.sentinel.Controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @GetMapping("/demo")
    @SentinelResource(value = "demo", blockHandler = "blockHandler", fallback = "fallback")
    public String demo(Integer id){

        return "Demo";
    }

    public String blockHandler(Integer id, BlockException e) {
        if (e instanceof FlowException) {
            return "当前请求过于火爆,已被流控";
        }

        if (e instanceof DegradeException) {
            return "当前请求过于火爆,已被降级";
        }

        if (e instanceof ParamFlowException) {
            return "当前请求过于火爆,已被热点参数限流";
        }

        return "服务器太火爆";
    }

    public String fallback(Integer id) {
        return "服务器错误";
    }
}
